package com.wanban.adutils.advertisers.admob

import android.app.Activity
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.wanban.adutils.AdUtils
import com.wanban.adutils.advertisers.BaseAdLoader
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.tools.AdConstants

/**
 * admob的插屏使用过一次就得重新加载
 */
class AdmobInterstitialAdUtils: BaseAdLoader() {

    private var interstitialAd: InterstitialAd? = null

    override fun loadAdNew(activity: Activity, adInfo: AdInfo, callback: OnLoadAdCallback) {
        super.loadAdNew(activity, adInfo, callback)
        InterstitialAd.load(activity, realUnitId, AdUtils.getAdRequest(),
            object : InterstitialAdLoadCallback() {
                override fun onAdLoaded(interstitialAd: InterstitialAd) {
                    super.onAdLoaded(interstitialAd)
                    if (!isTimeout) {
                        sdkLoadFinish()
                    }
                    setAdLoadSuccess()
                    interstitialAd.fullScreenContentCallback = object : FullScreenContentCallback() {
                            override fun onAdDismissedFullScreenContent() {
                                super.onAdDismissedFullScreenContent()
                                setAdLoadShowing(false)
                                callback.onAdComplete()
                                destroy()
                            }

                            override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                                super.onAdFailedToShowFullScreenContent(p0)
                                val code = p0.code
                                setAdLoadShowing(false)
                                callback.onAdLoadFailed(code)
                                analysisErrorCode(adInfo, code)
                                destroy()
                            }

                            override fun onAdShowedFullScreenContent() {
                                super.onAdShowedFullScreenContent()
                                setAdLoadShowing(true)
                                callback.onAdOpened()
                            }
                        }
                    this@AdmobInterstitialAdUtils.interstitialAd = interstitialAd

                    if (!isTimeout) {
                        callback.onAdLoaded(interstitialAd)
                    }
                }

                override fun onAdFailedToLoad(p0: LoadAdError) {
                    super.onAdFailedToLoad(p0)
                    if (isTimeout) {
                        destroy()
                        return
                    }
                    sdkLoadFinish()
                    val code = getAdmobErrorCode(p0)
                    analysisErrorCode(adInfo, code)
                    callback.onAdLoadFailed(code)
                    destroy()
                }
            })
    }


    override fun isAdLoadSuccess(): Boolean {
        return super.isAdLoadSuccess() && interstitialAd != null
    }

    override fun showAd(activity: Activity) {
        super.showAd(activity)
        if (interstitialAd == null) {
            mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_INVALID_REQUEST)
            destroy()
            return
        }
        interstitialAd?.show(activity)
    }

    override fun destroy() {
        super.destroy()
        interstitialAd = null
    }
}